def deployNpm(buildConfig = null) {
    if (buildConfig == null) {
        echo "no buildConfig"
        return false
    }

    version = sh(script: "git rev-parse --short=10 HEAD", returnStdout: true).trim()
    subject = sh(script: "git log --pretty=format:%s -1 HEAD", returnStdout: true).trim()
    message = sh(script: "git log -1 | awk '{if(NR==5) print}'", returnStdout: true).trim()

    if (!message.contains("_____im30fe_npm_update______")) {
        echo "no _____im30fe_npm_update______"
        return true
    }

    author  = sh(script: "git log --pretty=format:%an -1 HEAD", returnStdout: true).trim()
    commitAt = sh(script: "git log -1 --pretty=format:%ct HEAD", returnStdout: true).trim().toLong()
    ct = new Date(commitAt * 1000)
    bt = new Date()

    List<Object> apps = buildConfig.getApps()
    def buildStatus = true
    for (Object app : apps) {
        echo "Try Build: ${app.name}@${version}"
        def exitValue = sh(script: "sh -x ${app.script}", returnStatus: true)
        if(exitValue != 0){
            echo "buildStatus is not 0"
            buildStatus = false
        }
    }
    return buildStatus
}

def publishNpm(buildConfig = null) {
    if (buildConfig == null) {
        return
    }

    version = sh(script: "git rev-parse --short=10 HEAD", returnStdout: true).trim()
    subject = sh(script: "git log --pretty=format:%s -1 HEAD", returnStdout: true).trim()
    message = sh(script: "git log -1 HEAD", returnStdout: true).trim()
    author  = sh(script: "git log --pretty=format:%an -1 HEAD", returnStdout: true).trim()
    commitAt = sh(script: "git log -1 --pretty=format:%ct HEAD", returnStdout: true).trim().toLong()
    ct = new Date(commitAt * 1000)
    bt = new Date()

    List<Object> apps = buildConfig.getApps()
    for (Object app : apps) {
        echo "Try Build: ${app.name}@${version}"
        sh "sh -x ${app.script}"
    }
}

def getCommitId() {
    version = sh(script: "git rev-parse --short=10 HEAD", returnStdout: true).trim()
    return version
}

def sonar(projectName, branchs = ['master']) {
    if (branchs.contains(env.BRANCH_NAME)) {
        sh "sonar-scanner -Dsonar.host.url=http://sonar-tx.http.svc.pre.ali.im30 -Dsonar.sourceEncoding=UTF-8 -Dsonar.projectKey=FE_${projectName} -Dsonar.sources=. -Dsonar.login=27023b0400857c5d5ae67f312fcd154006a020ce -Dsonar.exclusions=**/node_modules/**,**/dist/**,**/webapps/**,**/_kfe_buildcache/**"
    }
}

def lint(projectName, branchs = ['master']) {
    if (branchs.contains(env.BRANCH_NAME)) {
        sh "npm install @im30fe/plugin-lint && npm run prdlint -- --banana=${projectName} --commitid=${getCommitId()}"
    }
}

def depscollect(projectName, currBranch) {
    if (currBranch == 'master') {
        sh "npm run depscollect -- --banana=${projectName} --commitid=${getCommitId()}"
    }
}

def scancollect(projectName, currBranch) {
    if (currBranch == 'master') {
        sh "npm run scancollect -- --banana=${projectName} --commitid=${getCommitId()}"
    }
}
